<?php
/**
 * @Author: ant
 * @time:2018-1-23
 * @Effect: 轮播图处理
 */

namespace app\admin\controller;

use app\admin\model\Banner as A;
use app\admin\model\BannerM as B;
use app\common\lib\upload;
use app\admin\model\ProductTypes;

class Banner  extends Common
{

    private  $PageTitle;
	//展示轮播分类
    public function img()
    {
        return $this->fetch();
    }
   public function index()
   {

     $this->PageTitle='轮播图';
   	//banner分类
   	$banner=new A();
   	$bannerData=$banner->getBannerTypeDateStates();
   	//halt($bannerData);
   	$this->assign('bannerdata',$bannerData);
   	//获得产品分类列表
       $arr=(new ProductTypes())->getAllData();
       $this->assign('arr',$arr);
   	$pid=input('param.pid');
    $pid= $pid ? $pid : 1;

    $BannerModular=$banner->getBannerModularDate($pid);
    $BannerIdData=$banner->getBannerTypeIdData($pid); 
    $this->assign('BannerD',$BannerIdData);
    $this->assign('BannerM',$BannerModular); 
    //halt($BannerModular);
       $this->generalInfor($this->PageTitle);
   	return $this->fetch();
   }
   //banner 分类展示
    public function banner_type()
    {
        $this->PageTitle='轮播图分类';
        $this->generalInfor($this->PageTitle);
        $banner=(new A())->getBannerTypeDate();
        $this->assign('banner',$banner);
        return $this->fetch();
    }
   //增加轮播图分类
   public function add_bannertype()
   {
       $this->PageTitle='添加轮播图分类';
       if(input('param.id'))
       {
           $this->PageTitle='修改轮播图分类';
           $banner=(new A())->getBannerTypeInfo(input('param.id'));
       }else
           {
               $banner['banner_id']='';
               $banner['banner_name']='';
               $banner['banner_description']='';
               $banner['banner_sort']='';
           }
       $this->assign('banner',$banner);
       $this->generalInfor($this->PageTitle);
   		if(request()->isPost())
            {
				//halt($_POST);
               $banner=new A();
                $res=$banner->add_banner(input('post.'));
                    $mess=array(
                        'status'=>$res['valid'],
                        'message'=>$res['msg']
                    );
                    return $mess;
            }

   	    return $this->fetch();
   }
   /**
    * 上传图片
    */
   public function localuploadImg(){
   	   //halt($_POST);
    // 获取表单上传文件 例如上传了001.jpg
       $path ="/uploads/";
        $file = request()->file('images');
        //halt($file);
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        $path = $path . $info->getSaveName();
        
            // 成功上传后 返回上传信息
        return json(array('state'=>1,'path'=>$path));
        }else{
            // 上传失败返回错误信息
        return json(array('state'=>0,'errmsg'=>'上传失败'));
        }
  }
  public function uploadifyImg(){  
        //halt($_FILES);
         $image = Upload::image($_FILES,$_POST['urlRoute']); 
         //图片样式处理
         //$fops = 'imageView2/1/w/800/h/100';
         $fops="";
        // 捕获异常  
        try{  
            // 返回qiniu上的文件名  
           
        }catch(\Exception $e){  
            return json(['state'=>0,'message'=>$e->getMessage()]);  
        }  
        // 返回给uploadify插件状态  
        if($image){  
            $data = [  
                'state' => 1,  
                'message' => 'OK',  
                'path' => config('qiniu.image_url').'/'.$image.'?'.$fops,  
            ];  
            return json($data);exit;       
        }else{  
            return json(['state'=>0,'message'=>'上传失败']);  
        }  
    }  
  /**
   *添加轮播图
   */
  public function add_bannerM()
  {
  	  if(request()->isPost())
            {
				//halt($_POST);
               $banner=new B();
                $res=$banner->add_bannerM(input('post.'));
                    $mess=array(
                        'status'=>$res['valid'],
                        'message'=>$res['msg']
                    );
                    return $mess;
            }
  }
    public function upload(){
        $file = request()->file('file');
        halt($file);
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
        if($info){
            echo $info->getSaveName();
            die();
        }else{
            echo $file->getError();
            die();
        }
    }
    //修改
    public function edit()
    {
        if(request()->isPost())
        {
            //halt($_POST);
            $res=(new B())->edit_banner(input('post.'));
            $mess=array(
                'status'=>$res['valid'],
                'message'=>$res['msg']
            );
            return $mess;
        }
        //获得产品分类列表
        $type_arr=(new ProductTypes())->getAllData();
        $this->assign('type_arr',$type_arr);
        $bannerData=(new A())->getBannerTypeDateStates();
        //halt($bannerData);
        $this->assign('bannerdata',$bannerData);
        $this->PageTitle='banner修改';
        $this->generalInfor($this->PageTitle);
        $id=input('param.id');
        $arr=(new A())->getOneInfo($id);
        $this->assign('arr',$arr);
        $this->generalInfor($this->PageTitle);

        return $this->fetch();
    }
    //下架
    public function shelf()
    {
        $res=(new B())->shelf(input('param.'));
        $mess = array(
            'status' => $res['valid'],
            'message' => $res['msg']
        );
        return $mess;
    }
    //分类下架
    public function type_shelf()
    {
        $res=(new A())->shelf(input('param.'));
        $mess = array(
            'status' => $res['valid'],
            'message' => $res['msg']
        );
        return $mess;
    }

}